package com.dxy.product.controller;

import com.dxy.common.vo.Result;
import com.dxy.common.pojo.Product;
import com.dxy.product.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/findProductById/{productId}")
    public Result<Product> findProductById(@PathVariable("productId") String productId) {
        Product product = productService.getById(productId);
        return Result.ok(product);
    }

    @GetMapping("/reduceStock/{productId}/{num}")
    public Result reduceStock(@PathVariable("productId") String productId, @PathVariable("num") int num) {
        int row = productService.reduceStock(productId, num);
        if (row > 0) {
            return Result.ok();
        } else {
            return Result.fail("库存不足");
        }
    }

    @GetMapping("/deleteProduct/{productId}")
    public Result deleteProduct(@PathVariable("productId") String productId) {
        productService.removeById(productId);
        return Result.ok();
    }

}
